package cms.simmytech.service;

import cms.simmytech.mapper.TbActivityMapper;
import cms.simmytech.mapper.TbActivityUserMapper;
import cms.simmytech.mapper.TbContentMapper;
import cms.simmytech.model.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class ActivityService {
    @Autowired
    private TbActivityMapper mapper;
    @Autowired
    private TbActivityUserMapper activityUserMapper;
    public PageInfo<TbActivity> getPageList(Integer pageNum, Integer pageSize, String name,Integer collegeId,String userName,Byte activityState) {
        TbActivityExample example=new TbActivityExample();
        TbActivityExample.Criteria criteria = example.createCriteria();
        //对标题名称进行处理--便于模糊查询
        if(!"".equals(name)&&null!=name){
            criteria.andActivityNameLike("%"+name+"%")	;
        }
        if (collegeId!=null){
            criteria.andCollegeIdEqualTo(collegeId);
        }
        if(!"".equals(userName)&&null!=userName){
            criteria.andUserNameEqualTo(userName);
        }
        if (activityState!=null){
            criteria.andActivityStateEqualTo(activityState);
        }
        //根据contentId进行降序
        example.setOrderByClause("activity_id  desc");
        PageHelper.startPage(pageNum, pageSize);
        List<TbActivity> list=mapper.selectByExample(example);
        PageInfo<TbActivity> pageInfo = new PageInfo<TbActivity>(list);
        return pageInfo;
    }
    public List<String> getUserNameList(){
        return mapper.selectUserNameList();
    }
    public TbActivity findActivityById(Integer activityId){
        TbActivity result=new TbActivity();
        try{
            result=mapper.selectByPrimaryKey(activityId);
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }
    public Boolean deleteActivityById(Integer activityId){
        if(activityId!=null){
            mapper.deleteByPrimaryKey(activityId);
            return true;
        }else{
            return false;
        }
    }

    public List<TbActivityUser> getActivityUserListById(Integer activityId) {

        TbActivityUserExample example=new TbActivityUserExample();
        example.createCriteria().andActivityIdEqualTo(activityId);
        return activityUserMapper.selectByExample(example);
    }

    public List<TbActivity> getAll() {
        return mapper.selectByExample(null);
    }

    public void updateActivityById(TbActivity tb) {
        mapper.updateByPrimaryKey(tb);
    }
}
